package org.argus.management.system.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import lombok.Data;

import java.util.List;

/**
 * @Author 刘文/Cliff Liu
 * @Date 2024/6/21
 */
@ApiModel(value = "菜单对应的路由")
@Data
public class MenuRoute implements Comparable<MenuRoute>{

    /**
     * 满足pure-admin前端要求，固定使用 place-holder 作为有权限菜单固定角色
     */
    public static final Long USER_MENUS_ROLE_PLACE_HOLDER = -996L;

    @JsonIgnore
    private Long id;

    private String name;

    @JsonIgnore
    private Long parentId;

    private String path;

    private MenuMeta meta;

    private List<MenuRoute> children;

    @Data
    public static class MenuMeta {
        private String title;
        private String icon;

        private Integer rank;

        /**
         * 满足pure-admin前端要求，固定使用 place-holder 作为有权限菜单固定角色
         */
        private Long[] roles = new Long[] {USER_MENUS_ROLE_PLACE_HOLDER};
    }

    @Override
    public int hashCode() {
        return id.hashCode();
    }

    @Override
    public boolean equals(Object another) {
        return another instanceof MenuRoute && (id.equals(((MenuRoute)another).id));
    }

    /**
     * 菜单排序
     * @param o
     * @return
     */
    @Override
    public int compareTo(MenuRoute o) {
        if (meta.getRank() != null && o.meta.getRank() != null) {
            if (meta.getRank().equals(o.meta.getRank())) {
                return id.compareTo(o.id);
            }
            return meta.getRank().compareTo(o.meta.getRank());
        }
        if (meta.getRank() != null) {
            return 1;
        }
        if (o.getMeta().getRank() != null) {
            return -1;
        }
        return 0;
    }

}
